package arrayList_basics.src.demo;

import java.util.ArrayList;
import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
//        学生添加
//        ArrayList<Student> list = new ArrayList();
//        for (int i = 0; i < 3; i++) {
//            Scanner sc = new Scanner(System.in);
//            Student s = new Student();
//            System.out.println("请输入姓名");
//            String name = sc.nextLine();
//            System.out.println("请输入年龄");
//            int age = sc.nextInt();
//            s.setName(name);
//            s.setAge(age);
//            list.add(s);
//        }
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i).getName()+","+list.get(i).getAge());
//        }
//        判断用户是否存在
        ArrayList<UserInfo> list = new ArrayList();
        UserInfo u1 = new UserInfo("111","zhangsan","123456");
        UserInfo u3 = new UserInfo("222","lisi","123456");
        UserInfo u2 = new UserInfo("333","wangwu","123456");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入id");
        String scId = sc.nextLine();
        System.out.println(equalsList(list,scId));
        equalsListObject(list,scId);

    }
    public static Boolean equalsList(ArrayList<UserInfo> list, String id) {
        return equalsListIndex(list,id)>=0;// 查找返回布尔值
    }
    public static int equalsListIndex(ArrayList<UserInfo> list, String id) { // 有返回索引,没有返回-1
        for (int i = 0; i < list.size(); i++) {
            boolean flag = list.get(i).getId().equals(id);
            if (flag) {
                return i;
            }
        }
        return -1;
    }
    public static ArrayList<UserInfo> equalsListObject(ArrayList<UserInfo> list, String id) { // 返回数据信息
        ArrayList<UserInfo> arrList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            boolean flag = list.get(i).getId().equals(id);
            if (flag) {
                arrList.add(list.get(i));
            }
        }
        for (int i = 0; i < arrList.size(); i++) {
            System.out.println(arrList.get(i).getName()+","+arrList.get(i).getPassword());
        }
        return arrList;
    }
}
